package fun.sanguo.service.record.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import fun.sanguo.mapper.record.RecordFactionMapper;
import fun.sanguo.model.dto.PageInfo;
import fun.sanguo.model.dto.PageInsert;
import fun.sanguo.model.dto.PageQuery;
import fun.sanguo.model.dto.PageUpdate;
import fun.sanguo.model.dto.record.RecordFactionInsert;
import fun.sanguo.model.dto.record.RecordFactionParam;
import fun.sanguo.model.dto.record.RecordFactionUpdate;
import fun.sanguo.model.entity.record.RecordFactionEntity;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.record.RecordFactionService;
import fun.sanguo.utils.DateUtils;
import fun.sanguo.utils.IdUtils;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 内容管理-势力列表 Service实现类
 *
 * @author ZYP
 * @date 2023-04-25
 */
@Service
public class RecordFactionServiceImpl implements RecordFactionService {

    @Resource
    private RecordFactionMapper recordFactionMapper;

    /**
     * 查询页面table列表
     *
     * @param pageQuery
     * @return
     * @throws ArtificialException
     */
    @Override
    public PageInfo<RecordFactionEntity> getPage(PageQuery<RecordFactionParam> pageQuery) throws ArtificialException {
        // 查询参数
        RecordFactionEntity recordFactionEntity = new RecordFactionEntity();
        // 转换查询参数
        try {
            // 接收参数
            RecordFactionParam recordFactionParam = new ObjectMapper().convertValue(pageQuery.getParam(), RecordFactionParam.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordFactionParam, recordFactionEntity);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 查询数据并封装返回
        try {
            // 查询总量
            int total = recordFactionMapper.getTotal(recordFactionEntity);
            // 查询列表（分页）
            List<RecordFactionEntity> recordFactionEntityList = recordFactionMapper.getList(recordFactionEntity, pageQuery);
            // 封装并返回页面信息
            return new PageInfo<>(recordFactionEntityList, total, pageQuery);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 新增
     *
     * @param pageInsert
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean insert(PageInsert<RecordFactionInsert> pageInsert) throws ArtificialException {
        // 新增参数
        RecordFactionEntity recordFactionEntity = new RecordFactionEntity();
        // 转换新增参数
        try {
            // 接收参数
            RecordFactionInsert recordFactionInsert = new ObjectMapper().convertValue(pageInsert.getParam(), RecordFactionInsert.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordFactionInsert, recordFactionEntity);
            // 其他参数赋值
            recordFactionEntity.setId(String.valueOf(new IdUtils().nextId()));
            recordFactionEntity.setCreateBy("system");
            recordFactionEntity.setCreateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordFactionEntity.setUpdateBy("system");
            recordFactionEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordFactionEntity.setVersion(1);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 新增数据并得到操作结果
        try {
            // 新增
            int amount = recordFactionMapper.insert(recordFactionEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 编辑
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean update(PageUpdate<RecordFactionUpdate> pageUpdate) throws ArtificialException {
        // 编辑参数
        RecordFactionEntity recordFactionEntity = new RecordFactionEntity();
        // 转换编辑参数
        try {
            // 接收参数
            RecordFactionUpdate recordFactionUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordFactionUpdate.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordFactionUpdate, recordFactionEntity);
            // 其他参数赋值
            recordFactionEntity.setUpdateBy("system");
            recordFactionEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 编辑数据并得到操作结果
        try {
            // 编辑
            int amount = recordFactionMapper.update(recordFactionEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 删除
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean delete(PageUpdate<RecordFactionUpdate> pageUpdate) throws ArtificialException {
        // 删除参数
        RecordFactionEntity recordFactionEntity = new RecordFactionEntity();
        // 转换删除参数
        try {
            // 接收参数
            RecordFactionUpdate recordFactionUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordFactionUpdate.class);
            // 赋值到实体类
            recordFactionEntity.setId(recordFactionUpdate.getId());
            // 其他参数赋值
            recordFactionEntity.setUpdateBy("system");
            recordFactionEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 删除数据并得到操作结果
        try {
            // 删除
            int amount = recordFactionMapper.delete(recordFactionEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }
}
